/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller.b;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.DistributionTypeEnum;
import com.ykht.proj.coupon.Enum.LookRoleTypeEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.service.*;
import com.ykht.proj.coupon.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 服务员奖励记录表(待定)
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:17
 */
@RestController
@AllArgsConstructor
@RequestMapping("b/fxreward")
@Api(value = "fxreward", tags = "b端服务员奖励记录表(待定)管理")
@Slf4j
public class BxRewardController {

	private final FxRewardService fxRewardService;

	private final CnUserExtService cnUserExtService;
	/**
	 * 服务员 服务
	 */
	private final FxWaiterExtService fxWaiterExtService;
	/**
	 * 提现记录表
	 */
	private final FxWithdrawalService fxWithdrawalService;

	@ApiOperation(value = "账户概览", notes = "账户概览")
	@GetMapping("zhgl")
	public R<ZHGLVo> zhgl(BaseSearchTimeVo timeVo, UserTypeIdVo userTypeIdVo) {
		log.info("->账户概览>{}>{}", timeVo, userTypeIdVo);
		return R.ok(fxRewardService.zhgl(timeVo, userTypeIdVo));
	}

	@ApiOperation(value = "优惠券核销情况", notes = "优惠券核销情况")
	@GetMapping("couponUse")
	public R<CouponUseResp> couponUse(BaseSearchTimeVo timeVo, UserTypeIdVo userTypeIdVo) {
		log.info("->优惠券核销情况>{}>{}", timeVo, userTypeIdVo);
		return R.ok(fxRewardService.couponUse(timeVo, userTypeIdVo));
	}

	@ApiOperation(value = "核销金额排行top10", notes = "核销金额排行top10")
	@GetMapping("couponUseTop")
	public R<CouponUseTopVo> couponUseTop(BaseSearchTimeVo timeVo, UserTypeIdVo userTypeIdVo) {
		log.info("->核销金额排行top10>{}>{}", timeVo, userTypeIdVo);
		return R.ok(fxRewardService.couponUseTop(timeVo, userTypeIdVo));
	}

	@ApiOperation(value = "分销任务", notes = "分销任务")
	@GetMapping("fxrw")
	public R<FXRWVo> fxrw(BaseSearchTimeVo timeVo, UserTypeIdVo userTypeIdVo) {
		log.info("->分销任务>{}>{}", timeVo, userTypeIdVo);
		return R.ok(fxRewardService.fxrw(timeVo, userTypeIdVo));
	}

	@ApiOperation(value = "门店排行top10", notes = "门店排行top10")
	@GetMapping("shopRewardTop")
	public R shopRewardTop(FxRewardView fxRewardView) {
		Long sjId = SecurityUtils.getSjId();
		//公司维度
		if (SecurityUtils.getUser().getUserType().equals(LookRoleTypeEnum.LOOK_ROLE_TYPE_1.getValue()) || SecurityUtils.getUser().getUserType().equals(LookRoleTypeEnum.LOOK_ROLE_TYPE_0.getValue())) {
			fxRewardView.setCompanyId(sjId);
		}
//		品牌维度
		if (SecurityUtils.getUser().getUserType().equals(LookRoleTypeEnum.LOOK_ROLE_TYPE_2.getValue())) {
			fxRewardView.setBrandId(sjId);
		}
		return R.ok(fxRewardService.shopRewardTop(fxRewardView));


	}

	@ApiOperation(value = "服务员排行top10", notes = "服务员排行top10")
	@GetMapping("waiterRewardTop")
	public R waiterRewardTop(FxRewardView fxRewardView) {
		Long sjId = SecurityUtils.getSjId();
		if (SecurityUtils.getUser().getUserType().equals(LookRoleTypeEnum.LOOK_ROLE_TYPE_1.getValue()) || SecurityUtils.getUser().getUserType().equals(LookRoleTypeEnum.LOOK_ROLE_TYPE_0.getValue())) {
			fxRewardView.setCompanyId(sjId);
		}
		//品牌维度
		if (SecurityUtils.getUser().getUserType().equals(LookRoleTypeEnum.LOOK_ROLE_TYPE_2.getValue())) {
			fxRewardView.setBrandId(sjId);
		}
		//门店维度
		if (SecurityUtils.getUser().getUserType().equals(LookRoleTypeEnum.LOOK_ROLE_TYPE_3.getValue())) {
			fxRewardView.setShopId(sjId);
		}
		return R.ok(fxRewardService.waiterRewardTop(fxRewardView));
	}


	/**
	 * 获取服务员 佣金收益 统计
	 *
	 * @return R
	 */
	@ApiOperation(value = "获取服务员 佣金收益 统计")
	@GetMapping("getCommissionRevenues")
	public R<FxRewardVo> getCommissionRevenues(FxRewardVo fxRewardVo) {
		try {
			if (null != fxRewardVo.getOffsetDay()) {
				fxRewardVo.setCreateStaTime(LocalDateTime.of(LocalDate.now().plusDays(fxRewardVo.getOffsetDay()), LocalTime.MIN));
				fxRewardVo.setCreateEndTime(LocalDateTime.of(LocalDate.now().plusDays(fxRewardVo.getOffsetDay() == -1 ? fxRewardVo.getOffsetDay() : 0), LocalTime.MAX));
			}
			List<Long> waiterIdList = fxWaiterExtService.getWaiterIdList();
			fxRewardVo.setWaiterIdList(waiterIdList);
			fxRewardVo.setLookRoleType(1);
			fxRewardVo.setDistributionType(DistributionTypeEnum.DISTRIBUTION_TYPE_0.getValue());
			FxRewardVo fxReward1 = fxRewardService.getSumByMoneyAndFind(fxRewardVo);
			if (null != fxReward1) {
				fxRewardVo.setInvitationCommissionCount(fxReward1.getDistributionTypeMoney());
				fxRewardVo.setInvitationCount(fxReward1.getCountNumber());
			}
			fxRewardVo.setDistributionType(DistributionTypeEnum.DISTRIBUTION_TYPE_1.getValue());
			FxRewardVo fxReward2 = fxRewardService.getSumByMoneyAndFind(fxRewardVo);
			if (null != fxReward2) {
				fxRewardVo.setForwardCommissionCount(fxReward2.getDistributionTypeMoney());
				fxRewardVo.setForwardCount(fxReward2.getCountNumber());
			}
			fxRewardVo.setDistributionType(DistributionTypeEnum.DISTRIBUTION_TYPE_2.getValue());
			FxRewardVo fxReward3 = fxRewardService.getSumByMoneyAndFind(fxRewardVo);
			if (null != fxReward3) {
				fxRewardVo.setCouponCommissionCount(fxReward3.getDistributionTypeMoney());
				fxRewardVo.setCouponCount(fxReward3.getCountNumber());
			}
			fxRewardVo.setDistributionType(null);
/*			fxRewardVo.setCreateStaTime(null);
			fxRewardVo.setCreateEndTime(null);
			fxRewardVo.setFyId(null);*/
			FxRewardVo fxReward = fxRewardService.getSumByMoneyAndFind(fxRewardVo);
			if (null != fxReward) {
				fxRewardVo.setCommissionCount(fxReward.getDistributionTypeMoney());
				fxRewardVo.setWithdrawalAmount(fxReward.getDistributionTypeMoney());
				// 获取已经提现的佣金金额
				FxWithdrawalVo fxWithdrawal = new FxWithdrawalVo();
				fxWithdrawal.setWaiterIdList(waiterIdList);
				FxWithdrawalVo fxWithdrawalVo = fxWithdrawalService.getSumByMoneyCount(fxWithdrawal);
				if (null != fxWithdrawalVo && null != fxWithdrawalVo.getCumulativeWithdrawalMoney()) {
					fxRewardVo.setWithdrawalAmount(fxReward.getDistributionTypeMoney().subtract(fxWithdrawalVo.getCumulativeWithdrawalMoney()));
				}
			}
			return R.ok(fxRewardVo);
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed();
		}
	}

	/**
	 * 获取服务员 佣金收益统计下边的饭友列表
	 *
	 * @param page
	 * @param fxRewardVo
	 * @return
	 */
	@ApiOperation(value = "获取服务员 佣金收益统计下边的饭友列表", notes = "获取服务员 佣金收益统计下边的饭友列表")
	@GetMapping("/getCommissionRevenuesFanYouCount")
	public R<IPage<FxRewardVo>> getCommissionRevenuesFanYouCount(Page page, FxRewardVo fxRewardVo) {
		if (null != fxRewardVo.getOffsetDay()) {
			fxRewardVo.setCreateStaTime(LocalDateTime.of(LocalDate.now().plusDays(fxRewardVo.getOffsetDay()), LocalTime.MIN));
			fxRewardVo.setCreateEndTime(LocalDateTime.of(LocalDate.now().plusDays(fxRewardVo.getOffsetDay() == -1 ? fxRewardVo.getOffsetDay() : 0), LocalTime.MAX));
		}
		fxRewardVo.setWaiterIdList(fxWaiterExtService.getWaiterIdList());
		fxRewardVo.setDistributionType(DistributionTypeEnum.DISTRIBUTION_TYPE_1.getValue());
		fxRewardVo.setSort(1);
		// 获取服务员n
		IPage<FxRewardVo> waiterIPage = fxRewardService.getCommissionRevenuesFanYouCount(new Page(), fxRewardVo);
		FxRewardVo rewardVo = waiterIPage.getRecords().get(0);
		rewardVo.setPersonPhone(SecurityUtils.getUser().getPhone());
		rewardVo.setPersonName(SecurityUtils.getUser().getNickName());
		rewardVo.setHeadPortrait(SecurityUtils.getUser().getAvatar());
		fxRewardVo.setDistributionType(null);
		fxRewardVo.setSort(0);
		List<Integer> typeList = new ArrayList<>();
		typeList.add(DistributionTypeEnum.DISTRIBUTION_TYPE_0.getValue());
		typeList.add(DistributionTypeEnum.DISTRIBUTION_TYPE_2.getValue());
		fxRewardVo.setTypeList(typeList);
		IPage<FxRewardVo> personVoIPage = fxRewardService.getCommissionRevenuesFanYouCount(page, fxRewardVo);
/*		List<FxRewardVo> records = new ArrayList<>();
		records.add(rewardVo);
		records.addAll(personVoIPage.getRecords());
		personVoIPage.setRecords(records);*/
		return R.ok(personVoIPage);
	}


	/**
	 * 获取服务员 收益明细 统计信息
	 *
	 * @return R
	 */
	@ApiOperation(value = "获取服务员 收益明细 统计信息")
	@GetMapping("getCommissionRevenuesDetailed")
	public R<FxRewardVo> getCommissionRevenuesDetailed(FxRewardVo fxRewardVo) {
		try {
			if (null != fxRewardVo.getOffsetDay()) {
				fxRewardVo.setCreateStaTime(LocalDateTime.of(LocalDate.now().plusDays(fxRewardVo.getOffsetDay()), LocalTime.MIN));
				fxRewardVo.setCreateEndTime(LocalDateTime.of(LocalDate.now().plusDays(fxRewardVo.getOffsetDay() == -1 ? fxRewardVo.getOffsetDay() : 0), LocalTime.MAX));
			}
			fxRewardVo.setWaiterIdList(fxWaiterExtService.getWaiterIdList());
			List<Integer> typeList = new ArrayList<>();
			typeList.add(DistributionTypeEnum.DISTRIBUTION_TYPE_0.getValue());
			typeList.add(DistributionTypeEnum.DISTRIBUTION_TYPE_2.getValue());
			fxRewardVo.setTypeList(typeList);
			FxRewardVo fxRewardVo1 = fxRewardService.getCommissionRevenuesDetailed(fxRewardVo);
			return R.ok(fxRewardVo1);
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed();
		}
	}


	/**
	 * 获取服务员 收益明细 的列表
	 *
	 * @param page
	 * @param fxRewardVo
	 * @return
	 */
	@ApiOperation(value = "获取服务员 收益明细 的列表", notes = "获取服务员 收益明细 的列表")
	@GetMapping("/getCommissionRevenuesFanYouDetailedList")
	public R<IPage<FxRewardVo>> getCommissionRevenuesFanYouDetailedList(Page page, FxRewardVo fxRewardVo) {
		if (null != fxRewardVo.getOffsetDay()) {
			fxRewardVo.setCreateStaTime(LocalDateTime.of(LocalDate.now().plusDays(fxRewardVo.getOffsetDay()), LocalTime.MIN));
			fxRewardVo.setCreateEndTime(LocalDateTime.of(LocalDate.now().plusDays(fxRewardVo.getOffsetDay() == -1 ? fxRewardVo.getOffsetDay() : 0), LocalTime.MAX));
		}
		fxRewardVo.setWaiterIdList(fxWaiterExtService.getWaiterIdList());
		IPage<FxRewardVo> personVoIPage = fxRewardService.getCommissionRevenuesFanYouDetailedList(page, fxRewardVo);
		return R.ok(personVoIPage);
	}


	/**
	 * 获取服务员 收益明细 的类型 统计
	 *
	 * @param fxRewardVo
	 * @return
	 */
	@ApiOperation(value = "获取服务员 收益明细 的类型 统计", notes = "获取服务员 收益明细 的类型 统计")
	@GetMapping("/getCommissionRevenuesFanYouDetailedCount")
	public R<FxRewardVo> getCommissionRevenuesFanYouDetailedCount(FxRewardVo fxRewardVo) {
		fxRewardVo.setWaiterIdList(fxWaiterExtService.getWaiterIdList());
		FxRewardVo fxRewardVo1 = fxRewardService.getCommissionRevenuesFanYouDetailedCount(fxRewardVo);
		return R.ok(fxRewardVo1);
	}

}
